/*
** video-filters 
*/
$(document).ready(function(e){
	var dialogCountry, 
		form = $('form[name=filter-form]');
	if(form.length < 1){
		return false;
	}else{
		form.attr('autocomplete', 'off');
	}
	
	TVB.reportOpen = function(url){
		var width=960, left = $(window).width()/2 - width/2, height=$(window).height();
		var a = window.open(url, 'result', 'width='+width+',height='+height+',top=0,left='+left+',toolbar=no,menubar=no,scrollbars=yes,resizable=no');
		a.focus();
	};
	
	$('input[name=submit]', form).bind('click', function(e){
		e.preventDefault();
		var dateby = function(){
			var v = '';
			$('input[name=dateby]', form).each(function(){
				if(this.checked){
					v= this.value;
				}
			});
			return v;
		}();
		var _getVal =function(mt, key){
			if(mt.length==0){
				return '';
			}
			//if(jQuery.trim( mt.val() ) == ''){return '';}
			return '&'+(key ? key : mt.attr('name'))+'='+ encodeURI( mt.val() );
		};
		var url = form.attr('action');
		var data = (dateby ? 'dateby='+ dateby : '')
			//+'&top_size='+ $('select[name=top_size]', form).val()
			+_getVal( $('select[name=top_size]', form) )
			//+'&month='+ $('select[name=month]', form).val()
			+_getVal( $('select[name=month]', form) )
			//+'&year='+ $('select[name=year]', form).val()
			+_getVal( $('select[name=year]', form) )
			+_getVal( $('select[name=day]', form) )
			//+'&startmonth='+ $('select[name=startmonth]', form).val()
			+_getVal( $('select[name=startmonth]', form) )
			//+'&startyear='+ $('select[name=startyear]', form).val()
			+_getVal( $('select[name=startyear]', form) )
			//+'&endmonth='+ $('select[name=endmonth]', form).val()
			+_getVal( $('select[name=endmonth]', form) )
			//+'&endyear='+ $('select[name=endyear]', form).val()
			+_getVal( $('select[name=endyear]', form) )
			+(form[0]['datezone[]'] ? '&startdate='+ form[0]['datezone[]'][0].value : '')
			+(form[0]['datezone[]'] ? '&enddate='+ form[0]['datezone[]'][1].value : '')
			//+'&age='+ $('select[name=age]', form).val()
			+_getVal( $('select[name=age]', form) )
			//+'&category='+ $('select[name=category]', form).val()
			+_getVal( $('select[name=category]', form) )
			//+'&gender='+ $('select[name=gender]', form).val()
			+_getVal( $('select[name=gender]', form) )
			//+'&plan='+ $('select[name=plan]', form).val()
			+_getVal( $('select[name=plan]', form) )
			//+'&package='+ $('select[name=package]', form).val()
			+_getVal( $('select[name=package]', form) )
			//+'&country='+ encodeURI($('input[name=countrys]', form).val())
			+_getVal( $('input[name=countrys]', form), 'country')
			//+'&sortby='+ $('select[name=sort]', form).val()
			+_getVal( $('select[name=sort]', form), 'sortby' )
			+_getVal( $('input[name=date]', form), 'date' )
			+_getVal( $('input[name=os-browser]', form), 'os_browser' )
			+_getVal( $('select[name=bycountry]', form) )
			+_getVal( $('select[name=product]', form) )
			+_getVal( $('select[name=top]', form) )
			+_getVal( $('select[name=firstlogin]', form) );
			
		TVB.reportOpen(url+'?'+data);
	});
	
	$('.datezone', form).addClass('cms-form-item-hidden');
	$('.dateby input', form).bind('click', function(e){
		if(this.value == 'month'){
			$('.bymonth', form).removeClass('cms-form-item-hidden');
			$('.bymonth2', form).removeClass('cms-form-item-hidden');
			$('.datezone', form).addClass('cms-form-item-hidden');
		}else{
			$('.bymonth', form).addClass('cms-form-item-hidden');
			$('.bymonth2', form).addClass('cms-form-item-hidden');
			$('.datezone', form).removeClass('cms-form-item-hidden');
		}
	});
	$('.member-dateby input', form).bind('click', function(e){
		if(this.value == 'month'){
			$('select[name=month]', form).css('display', 'none');
			$('select[name=day]', form).css('display', 'none');
		}else if(this.value == 'date'){
			$('select[name=month]', form).css('display', 'inline');
			$('select[name=day]', form).css('display', 'none');
		}else if(this.value == 'hour'){
			$('select[name=month]', form).css('display', 'inline');
			$('select[name=day]', form).css('display', 'inline');
		}
	});
	$('select.month', form).each(function(){
		$('option', this).each(function(){
			if( $(this).attr('value') == $('input.datebymonth', form).attr('month')){
				$(this).attr('selected', 'selected');
			}else{
				$(this).removeAttr('selected');
			}
		});
	});
	$('select.year', form).each(function(){
		$('option', this).each(function(){
			if( $(this).attr('value') == $('input.datebymonth', form).attr('year')){
				$(this).attr('selected', 'selected');
			}else{
				$(this).removeAttr('selected');
			}
		});
	});
	//
	var mplan = $('select[name=plan]', form);
	if(mplan.length){
		var s = $('select[name=package]', form).parent().parent();
		if(mplan.val() == 'free'){
			s.addClass('cms-form-item-hidden');
		}
		mplan.bind('change', function(e){
			s.toggleClass('cms-form-item-hidden');
		});
	}
	
	$("#form-month-top select").bind("change", function(e){
		$("#form-month-top").submit();
	});

	$(".list .tabs li").bind("click", function(e){
		var t = $(this), input=$(this).find('input'), type = t[0].id;
		if(t.find('input').hasClass('selected')){
			return false;
		}
		$(".list .tabs li input").removeClass('selected');
		input.addClass('selected');
		
		$("#form-month-top input[name='actions']").val(input.val());
		$("#form-month-top").submit();
	});
	$('#bd .snav a').hover(function(e){
		$(this).addClass('ui-state-hover');
	}, function(e){
		$(this).removeClass('ui-state-hover');
	});
	$('#bd .snav .datepicker').datepicker({
		dateFormat: 'yy-mm-dd'
	});
	
	$('.add-os-browser', form).bind('click', function(e){
		var s=true,v = $('input[name=os-browser]', form).val().split('\n'), currv=$('select[name=os]', form).val() +' + '+ $('select[name=browser]', form).val();
		var ilen=v.length, mv=[];
		for(var i=0; i<ilen; i++){
			if(jQuery.trim(v[i]) != ''){
				mv.push(v[i]);
			}
			if(v[i] == currv){s=false;}
		}
		if(s){
			mv.push(currv);
			$('.os-browser-item .list ul', form).append('<li>'+currv+'<span class="ui-icon ui-icon-close">&nbsp;</span></li>');
		}
		$('input[name=os-browser]', form).val(mv.join('\n'));
	});
	
	$('.all-os-browser', form).bind('click', function(e){
		var os = $('select[name=os] option', form), browser=$('select[name=browser] option', form),
			mv=[];
		$('.os-browser-item .list ul', form).html('');
		for(var i=0, ilen=os.length; i<ilen; i++){
			for(var j=0, jlen=browser.length; j<jlen; j++){
				var currv = os.eq(i).attr('value') +' + '+ browser.eq(j).attr('value');
				mv.push( currv );
				$('.os-browser-item .list ul', form).append('<li>'+currv+'<span class="ui-icon ui-icon-close">&nbsp;</span></li>');
			}
		}
		$('input[name=os-browser]', form).val(mv.join('\n'));
	});
	
	$('.reset-os-browser', form).bind('click', function(e){
		$('input[name=os-browser]', form).val('');
		$('.os-browser-item .list ul', form).html('');
	});
	
	$('.os-browser-item .list', form).bind('click', function(e){
		var t=$(e.target);
		if(t.hasClass('ui-icon-close')){
			t.parent().remove();
			var lis = $('.os-browser-item .list ul li', form), mv=[];
			for(var i=0, ilen=lis.length; i<ilen; i++){
				mv.push( lis.eq(i).text() );
			}
			$('input[name=os-browser]', form).val(mv.join('\n'));
		}
	});
	
	/*
	** 
	*/
	var mlis = $('form[name=filter-form] .datezone .cms-datepicker');
	mlis.eq(0).bind('change', function(e){
		var d = new Date(mlis.eq(0).val());
		mlis.eq(1).datepicker('option', 'maxDate', new Date( d.getTime() + 45*24*3600*1000 ) );
		mlis.eq(1).datepicker('option', 'minDate', d );
	});
	mlis.eq(1).datepicker('option', 'maxDate', new Date( new Date(mlis.eq(0).val()).getTime() + 45*24*3600*1000 ) );
	mlis.eq(1).datepicker('option', 'minDate', new Date(mlis.eq(0).val()) );
	
});



/*
** result page(print)
*/
(function(){
	var scope = $('#result');
	if(scope.length < 1){return false;}
	
	if(scope.hasClass('reg_active_confirm_result') || scope.hasClass('login_out_result') || scope.hasClass('forget_password_result') || scope.hasClass('edit_profile_result')){
		$(".bd input.columns", scope).each(function(){
			var t=$(this);
			
			var ctx = new TVB.canvasColumns({
				refEl: t,
				title: t.attr('title'),
				labels: eval('(' + t.attr('labels') + ')'),
				captures: eval('(' + t.attr('captures') + ')'),
				colors: (t.attr('colors') ? eval('(' + t.attr('colors') + ')') : []),
				datas: eval('(' + t.attr('datas') + ')')
			});
		});
	}
	if(scope.hasClass('os_browser_result')){
		$(".bd dd input.countrychart", scope).each(function(){
			var t=$(this), id=TVB.util.genId('chart_'), canvasEl;
			canvasEl = $('<canvas id="'+id+'" width="500" height="300"></canvas>');
			canvasEl.insertBefore(t);
			
			var ctx = new TVB.canvasCircleChart('#'+id, {
				x:160,y:160,radius:100,capture: t.attr('name'),
				datas: {
					data: eval('(' + t.attr('value') + ')'),
					color: (t.hasClass('none') ? ['#819BB0'] : []),
					capture: eval('(' + t.attr('capture') + ')')
				}
			});
		});
		$(".bd dd input.countrychart2", scope).each(function(){
			var t=$(this), id=TVB.util.genId('chart_'), canvasEl;
			canvasEl = $('<canvas id="'+id+'" width="450" height="300"></canvas>');
			canvasEl.insertBefore(t);
			
			var ctx = new TVB.canvasCircleChart('#'+id, {
				x:120,y:140,radius:70,capture: t.attr('name'),
				datas: {
					data: eval('(' + t.attr('value') + ')'),
					color: (t.hasClass('none') ? ['#819BB0'] : []),
					capture: eval('(' + t.attr('capture') + ')')
				}
			});
		});
	}
	if(scope.hasClass('video_bitrate_result')){
		$(".bd dd input.countrychart", scope).each(function(){
			var t=$(this), id=TVB.util.genId('chart_'), canvasEl;
			canvasEl = $('<canvas id="'+id+'" width="400" height="200"></canvas>');
			canvasEl.insertBefore(t);
			
			var ctx = new TVB.canvasCircleChart('#'+id, {
				x:100,y:120,radius:50,capture: t.attr('name'),
				datas: {
					data: eval('(' + t.attr('value') + ')'),
					color: (t.hasClass('none') ? ['#819BB0'] : []),
					capture: eval('(' + t.attr('capture') + ')')
				}
			});
		});
	}
	if(scope.hasClass('subscription_result')){
		$(".bd input.canvasCurveLines", scope).each(function(){
			var t=$(this), id=TVB.util.genId('line_'), canvasEl;
			canvasEl = $('<canvas id="'+id+'" width="650" height="360"></canvas>');
			canvasEl.insertBefore(t);
			
			var ctx = new TVB.canvasCurveLines('#'+id, {
				datas: eval('(' + t.attr('value') + ')'),
				labels:eval('(' + t.attr('labels') + ')')
			});
		});
		$(".bd input.canvasCurveLines36", scope).each(function(){
			var t=$(this), id=TVB.util.genId('line_'), canvasEl;
			canvasEl = $('<canvas id="'+id+'" width="780" height="360"></canvas>');
			canvasEl.insertBefore(t);
			
			var ctx = new TVB.canvasCurveLines('#'+id, {
				datas: eval('(' + t.attr('value') + ')'),
				labels:eval('(' + t.attr('labels') + ')')
			});
		});
	}
	
	$('.print', scope).bind('click', function(e){
		e.preventDefault();
		window.print();
	});	
})();

/*
** daily page email
*/
(function(){
	var json = jQuery.parseJSON( $('#emailjson').val() );
	if(!json){return false;}
	json['pager']= '#email-pager';
	json['sortname'] = 'id';
	json['viewrecords'] = true;
	json['sortorder'] = "desc";
	json['height'] = 'auto';
	json['inlineEdit'] = true
	json['datatype'] = 'json';
	
	$('#email-list').jqGrid(json).navGrid('#email-pager',{edit:true,add:true,del:true});
	
	$('.daily-email form.FormGrid').validate({
		rules: {
			field: {
				required: true,
				email: true
			}
		}
	});

})();

(function () {
    var filter = TVB.util.scriptFilter;

    /* initialize components */
    TVB.Cms.uiInit(document);

})();

